package com.online.fund.spider;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.online.fund.spider.dao.FundDao;
import com.online.fund.spider.entity.FundAssetAllocation;
import com.online.fund.util.HttpClientUtil;

/**
 * 基金资产配置
 * @author galen.zhang
 *
 */
@Service
public class FundAssetAllocationPageProcesser {
	private final static String FUND_PAGE_URL = "http://fund.eastmoney.com/f10/zcpz_";
	private final static String FUND_PAGE_URL_SUFFIX = ".html";
	
	private final static Logger logger = LoggerFactory.getLogger(FundAssetAllocationPageProcesser.class);
	
	@Resource
	private FundDao fundDao;
	
	public void parsePage(String fundCode, String fundName) {
		try {
			String pageHtml = HttpClientUtil.httpGetRequest(FUND_PAGE_URL + fundCode + FUND_PAGE_URL_SUFFIX);
			Document doc = Jsoup.parse(pageHtml);
			Elements configDetailElements = doc.select("div.boxitem table tr");
			
			if (configDetailElements.size() > 1) {
				List<FundAssetAllocation> assetAllocationList = new ArrayList<FundAssetAllocation>();
				List<String> statisticsDateList = new ArrayList<String>();
				for (int i = 1; i < configDetailElements.size(); i++) {
					Elements tdElements = configDetailElements.get(i).select("td");
					FundAssetAllocation fundAssetAllocation = new FundAssetAllocation();
					fundAssetAllocation.setFundCode(fundCode);
					fundAssetAllocation.setFundName(fundName);
					String statisticsDate = tdElements.get(0).text();
					fundAssetAllocation.setStatisticsDate(statisticsDate);
					String shareNetRatio =  tdElements.get(1).text().replaceAll("%", "");
					String bondNetRatio =  tdElements.get(2).text().replaceAll("%", "");
					String cashNetRatio =  tdElements.get(3).text().replaceAll("%", "");
					String netAssets =  tdElements.get(4).text().replaceAll("%", "");
					
					if (!shareNetRatio.contains("-")) {
						fundAssetAllocation.setShareNetRatio(Double.parseDouble(shareNetRatio));
					}
					if (!bondNetRatio.contains("-")) {
						fundAssetAllocation.setBondNetRatio(Double.parseDouble(bondNetRatio));
					}
					if (!cashNetRatio.contains("-")) {
						fundAssetAllocation.setCashNetRatio(Double.parseDouble(cashNetRatio));
					}
					if (!netAssets.contains("-")) {
						fundAssetAllocation.setNetAssets(Double.parseDouble(netAssets));
					}
					
					assetAllocationList.add(fundAssetAllocation);
					statisticsDateList.add(statisticsDate);
//					System.out.println(fundAssetAllocation);
				}
				
				fundDao.deleteFundAssetAllocation(fundCode, statisticsDateList);
				fundDao.batchAddFundAssetAllocation(assetAllocationList);
			}
//			System.out.println(configDetailElements);
		} catch (Exception e) {
			logger.error(fundCode, e);
		}
		
	}
	
	public static void main(String[] args) throws IOException {
		FundAssetAllocationPageProcesser pageProcesser = new FundAssetAllocationPageProcesser();
		pageProcesser.parsePage("001423", "");
	}
}
